package com.zyx.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyx.mapper.SpencerMapper;
import com.zyx.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpencerService {
    @Resource
    private SpencerMapper spencerMapper;


    /**
     * 查询所有店铺数量和每日新增数量
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)//readOnly=true查询到的数据只读
    public Map<String,Integer> queryAll(){
        SpencerExample example = new SpencerExample();
        int l = spencerMapper.countByExample(example);
        int i = spencerMapper.queryByTime();
        Map<String,Integer> map = new HashMap<>();
        map.put("data_size",l);
        map.put("data_new",i);
        return map;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public int queryById(String spencerName){
        return spencerMapper.queryById(spencerName);
    }

    /**
     * 模糊查询和查询所有数据
     * @param pageNum
     * @param pageSize
     * @param spencer
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Spencer> queryAll(Integer pageNum, Integer pageSize, Spencer spencer){
        SpencerExample example = new SpencerExample();
        SpencerExample.Criteria criteria = example.createCriteria();
        if (spencer != null){
            //根据车辆名称进行模糊查询
            if (spencer.getSpencerName() != null && !"".equals(spencer.getSpencerName().trim())){
                criteria.andSpencerNameLike("%"+spencer.getSpencerName().trim()+"%");
            }
            if (spencer.getUserName() != null && !"".equals(spencer.getUserName().trim())){
                criteria.andUserNameLike("%"+spencer.getUserName().trim()+"%");//根据汽车品牌进行模糊查询
            }
            if (spencer.getLocation() != null && !"".equals(spencer.getLocation().trim())){
                criteria.andLocationEqualTo("%"+spencer.getLocation().trim()+"%");//根据车辆大小进行模糊查询
            }
            if (spencer.getCreateTime() != null){
                criteria.andCreateTimeEqualTo(spencer.getCreateTime());//根据车辆上市时间查询
            }
        }
        //分页
        PageHelper.startPage(pageNum,pageSize);//只对紧邻的查询语句有效，是用拦截器的方式实现
        List<Spencer> list = spencerMapper.selectByExample(example);
        return new PageInfo<>(list);
    }

    /**
     * 添加店铺信息
     * @param spencer
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})//REQUIRED 事务 rollbackFor遇到异常回滚
    public int addSpencer(Spencer spencer){
        return spencerMapper.insertSelective(spencer);
    }

    /**
     * 根据主键查询信息
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Spencer selectByKey(int id){
        return spencerMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据主键删除----逻辑删除：实际开发中一般不允许把数据从磁盘内删除
     * @param spencer
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteAuto(Spencer spencer){
        spencer.setSpencerId(3);
        int i = spencerMapper.updateByPrimaryKey(spencer);
        return i;
    }

    /**
     * 根据主键动态更新数据
     * @param spencer
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateSpencer(Spencer spencer){
        return spencerMapper.updateByPrimaryKeySelective(spencer);
    }

    /**
     * 管理员登录
     * @param spencer
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)//readOnly=true查询到的数据只读
    public List<Spencer> Login(Spencer spencer) {
        SpencerExample example = new SpencerExample();
        SpencerExample.Criteria criteria = example.createCriteria();
        List<Spencer> login = new ArrayList<>();
        if (spencer.getSpencerUser() != null && !"".equals(spencer.getSpencerUser().trim())){
            criteria.andSpencerUserEqualTo(spencer.getSpencerUser().trim());
        }
        if (spencer.getSpencerWord() != null && !"".equals(spencer.getSpencerWord().trim())){
            criteria.andSpencerWordEqualTo(spencer.getSpencerWord().trim());
        }
        login = spencerMapper.selectByExample(example);
        return login;
    }

    /**
     * @Description: 根据店铺主键查询信息
     * @Author: zyx
     * @Date: 19:47 2022/2/14
     * @Name: queryUser
     * @Param: [spencerId]
     * @Return: com.zyx.pojo.Spencer
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)//readOnly=true查询到的数据只读
    public Spencer queryUser(Integer spencerId){
        Spencer spencer = spencerMapper.selectByPrimaryKey(spencerId);
        return spencer;
    }
}
